package com.andorid.settings.fuelgauge;

import com.andorid.settings.fuelgauge.R.id;

import android.app.ActionBar;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Switch;
import android.widget.TextView;

public class PowerModeActivity extends PreferenceActivity implements OnClickListener {

	private static final String KEY_BRIGHTNESS_DIALOG = "key_brightness_dialog";
	private static final String KEY_BRIGHTNESS = "key_brightness";
	private static final String KEY_WIFI = "key_wifi";
	private static final String KEY_BLUETOOTH = "key_bluetooth";
	private static final String KEY_GPS = "key_gps";
	private static final String KEY_RINGTONE = "key_ringtone";
	private static final String KEY_VIBRATION = "key_vibration";

	private Preference mBrightnessLevel;
	private CheckBoxPreference mBrightness;
	private CheckBoxPreference mWifi;
	private CheckBoxPreference mBluetooth;
	private CheckBoxPreference mGps;
	private CheckBoxPreference mRingtone;
	private CheckBoxPreference mVibration;
	
	private Switch mSwitch;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.power_mode);

		this.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		this.getActionBar().setCustomView(R.layout.custom_actionbar);
		this.getActionBar().getCustomView().findViewById(R.id.back).setOnClickListener(this);
		TextView title = (TextView) this.getActionBar().getCustomView().findViewById(R.id.title);
		title.setText(getString(R.string.power_saving_mode_title));
		
		mSwitch = (Switch)this.getActionBar().getCustomView().findViewById(R.id.switch1);
		mSwitch.setOnCheckedChangeListener(mSwitchChangeListener);

		mBrightnessLevel = findPreference(KEY_BRIGHTNESS_DIALOG);
		mBrightness = (CheckBoxPreference) findPreference(KEY_BRIGHTNESS);
		mWifi = (CheckBoxPreference) findPreference(KEY_WIFI);
		mBluetooth = (CheckBoxPreference) findPreference(KEY_BLUETOOTH);
		mGps = (CheckBoxPreference) findPreference(KEY_GPS);
		mRingtone = (CheckBoxPreference) findPreference(KEY_RINGTONE);
		mVibration = (CheckBoxPreference) findPreference(KEY_VIBRATION);

		mBrightness.setOnPreferenceChangeListener(mOnPreferenceChangeListener);
		mWifi.setOnPreferenceChangeListener(mOnPreferenceChangeListener);
		mBluetooth.setOnPreferenceChangeListener(mOnPreferenceChangeListener);
		mGps.setOnPreferenceChangeListener(mOnPreferenceChangeListener);
		mRingtone.setOnPreferenceChangeListener(mOnPreferenceChangeListener);
		mVibration.setOnPreferenceChangeListener(mOnPreferenceChangeListener);
		
		updateStatus();
	}
	
	
	private void updateStatus() {
		if (mSwitch.isEnabled()) {
			mBrightnessLevel.setEnabled(true);
			mBrightness.setEnabled(true);
			mWifi.setEnabled(true);
			mBluetooth.setEnabled(true);
			mGps.setEnabled(true);
			mRingtone.setEnabled(true);
			mVibration.setEnabled(true);
		} else {
			mBrightnessLevel.setEnabled(false);
			mBrightness.setEnabled(false);
			mWifi.setEnabled(false);
			mBluetooth.setEnabled(false);
			mGps.setEnabled(false);
			mRingtone.setEnabled(false);
			mVibration.setEnabled(false);
		}
	}
	
	
	private OnCheckedChangeListener mSwitchChangeListener = new OnCheckedChangeListener(){

		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			// TODO Auto-generated method stub
			mBrightnessLevel.setEnabled(isChecked);
			mBrightness.setEnabled(isChecked);
			mWifi.setEnabled(isChecked);
			mBluetooth.setEnabled(isChecked);
			mGps.setEnabled(isChecked);
			mRingtone.setEnabled(isChecked);
			mVibration.setEnabled(isChecked);
		}
		
	};

	private OnPreferenceChangeListener mOnPreferenceChangeListener = new OnPreferenceChangeListener() {

		@Override
		public boolean onPreferenceChange(Preference preference, Object newValue) {
			// TODO Auto-generated method stub
			CheckBoxPreference checkpref = (CheckBoxPreference) preference;
			if (KEY_BRIGHTNESS.equals(preference.getKey())) {
				boolean b = (Boolean) newValue;
				checkpref.setChecked(b);

			} else if (KEY_WIFI.equals(preference.getKey())) {
				boolean b = (Boolean) newValue;
				checkpref.setChecked(b);

			} else if (KEY_BLUETOOTH.equals(preference.getKey())) {
				boolean b = (Boolean) newValue;
				checkpref.setChecked(b);

			} else if (KEY_GPS.equals(preference.getKey())) {
				boolean b = (Boolean) newValue;
				checkpref.setChecked(b);

			} else if (KEY_RINGTONE.equals(preference.getKey())) {
				boolean b = (Boolean) newValue;
				checkpref.setChecked(b);

			} else if (KEY_VIBRATION.equals(preference.getKey())) {
				boolean b = (Boolean) newValue;
				checkpref.setChecked(b);

			}

			return true;
		}

	};

	public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
		if (KEY_BRIGHTNESS_DIALOG.equals(preference.getKey())) {
			showDiaog();
			return true;
		}

		return super.onPreferenceTreeClick(preferenceScreen, preference);
	}

	private void showDiaog() {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setCustomTitle(LayoutInflater.from(this)
				.inflate(R.layout.custom_dialog_title, null));

		View view = LayoutInflater.from(this).inflate(R.layout.custom_dialog_view, null);

		final TextView tv = (TextView) view.findViewById(R.id.textView2);

		SeekBar seekbar = (SeekBar) view.findViewById(R.id.seekBar1);
		seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				// TODO Auto-generated method stub
				tv.setText(progress + "%");

			}

			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub

			}

		});

		builder.setView(view);
		builder.setNegativeButton(R.string.power_saving_cancel, null);
		builder.setPositiveButton(R.string.power_saving_ok, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				int value = Integer.parseInt(tv.getText().toString().replace("%", ""));
				mBrightnessLevel.setSummary(value + "%");
			}
		});
		builder.show();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v.getId() == R.id.back) {
			finish();
		}
	}

}
